<?php
/**
 * 考勤系统App请假记录相关接口
 */
namespace app\attendance\controller;

use app\attendance\Base;
use think\Config;
use think\Db;
use think\Loader;
use think\exception\HttpException;
use baseutils\BaseUtil; # 验证类

use com\gdl\kq\interfaces\AttAppLeaveServiceClient;
use com\gdl\kq\dataobject\leave\LeaveDTO; 
use com\gdl\kq\dataobject\leave\LeaveAllDTO; 

class AttAppLeave extends Base
{
	
	/**
	 * 提交请假申请
	 * @return [type] [description]
	 */
	public function addLeave()
    {
    	$msg =  ['msg'=>'网络异常，请检查手机网络','code'=>'400','data'=>[]];

		#其他参数
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : '';  #平台id
		$userId = $this->request->param('userId') ? getStr($this->request->param('userId')) : ''; #用户id
		$unitId =  $this->request->param('unitId') ? getStr($this->request->param('unitId')) : ''; #单位id
		$startTime = $this->request->param('startTime') ? getStr($this->request->param('startTime')) : ''; # 请假开始时间
		$endTime = $this->request->param('endTime') ? getStr($this->request->param('endTime')) : ''; # 请假结束时间
		$numbers = $this->request->param('numbers') ? getStr($this->request->param('numbers')) : 0; #累计请假时间
		$leave = $this->request->param('leave') ? getStr($this->request->param('leave')) : ''; #请假事由
		$type = $this->request->param('type') ? getStr($this->request->param('type')) : 0; #'请假类型 默认为0 1年假 2事假 3调休 4产假 5婚假 6丧假'

		#公共
		$token = $this->request->param('token') ? getStr($this->request->param('token')) : '';
		if(!checkToken($token,$userId)){$msg['msg'] ='用户登录失效，请重新登录'; $msg['code']=501; return json_encode($msg);}

		#验证
		if(empty($startTime)){$msg['msg'] = "请选择正确的开始时间"; return json_encode($msg);}
		if(empty($endTime)){$msg['msg'] = "请选择正确的结束时间"; return json_encode($msg);}
		if(empty($numbers) || $numbers<=0){$msg['msg'] = "请选择正确的请假时间"; return json_encode($msg);}
		if(empty($leave)){$msg['msg'] = "请填写请假事由"; return json_encode($msg);}

		#处理参数 将年月日处理成时间戳
		$startTime = strtotime($startTime);
		$endTime = strtotime($endTime);

		try {
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAppLeaveServiceClient(NULL);
			\ApiClient::build($api);
			#多图上传
			$upload = new Upload();
			$photo = $upload->qiniuManyUploadfile();

			#生成审批编号
			$numberId = date("YmdHis",time()).mt_rand(100000,999999);

			$leaveDO = new LeaveDTO();
			$sdkData = [
				'memberId'=>$userId,
				'numberId'=>$numberId,
				'startTime'=>$startTime,
				'endTime'=>$endTime,
				'numbers'=>$numbers,
				'leaveInfo' =>$leave,
				'leaveType'=>$type,
				'images'=>serialize($photo),
				'createTime'=>time(),
				'platformId'=>$platformId,
				'createTime'=>time(),
				'status'=>1, #审核状态：1审核中 2审核通过 3未通过 4撤销
				'urge'=>2,#是否催办：1：已催办 2：未催办
			];
			$leaveDO->userId = $userId; 
			$leaveDO->platformId = $platformId;
			$leaveDO->unitId = $unitId;
			$leaveDO->data = $sdkData;

            #调用接口
			$result = $api->addLeave($leaveDO);
			if($result->success==true){
				$msg['code'] = $result->code;
				$msg['msg'] = $result->message;
				if($result->data){
					$msg['data']['id'] = $result->data[0][0]?$result->data[0][0]:0;
				} 
			}
		} catch (Exception $ex) {
			 $msg['msg'] = $ex->getMessage();
		}

		return json_encode($msg);
	}

	//请假记录列表
	public function leaveList(){
		$msg =  ['msg'=>'请求网络失败，请检查手机网络状态','code'=>'400','data'=>[]];
		#其他参数
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : '';  #平台id
		$userId = $this->request->param('userId') ? getStr($this->request->param('userId')) : ''; #用户id
		$unitId =  $this->request->param('unitId') ? getStr($this->request->param('unitId')) : ''; #单位id
		$offset =  $this->request->param('offset') ? getStr($this->request->param('offset')) : 1; #分页 默认第一页
		$num =  $this->request->param('num') ? getStr($this->request->param('num')) : 10; #分页 默认10条
		$likeTitle =  $this->request->param('likeTitle') ? getStr($this->request->param('likeTitle')) : '';#模糊查询 支持编号 状态 正文
		$status =  $this->request->param('status') ? getStr($this->request->param('status')) : '';#状态 0：全部 1：审批完成 2：审批中 3：撤销

		#验证token userId 和 token 必传
		$token = $this->request->param('token') ? getStr($this->request->param('token')) : '';
		if(!checkToken($token,$userId)){$msg['msg'] ='用户登录失效，请重新登录'; $msg['code']=501; return json_encode($msg);}
		if(empty($platformId)){$msg['msg'] = "请选择正确的平台"; return json_encode($msg);}

		try{
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAppLeaveServiceClient(NULL);
			\ApiClient::build($api);
			#sdkData 
			$sdkData = ['likeTitle'=>$likeTitle, 'status'=>$status];
			$leaveDO = new LeaveAllDTO();
			$leaveDO->userId = $userId; 
			$leaveDO->platformId = $platformId;
			$leaveDO->unitId = $unitId;
			$leaveDO->offset = $offset;
			$leaveDO->num = $num;
			$leaveDO->data = $sdkData;
			$result = $api->leaveList($leaveDO);
			if($result->success == true){
				$msg['msg'] = $result->message;
				$msg['code'] = $result->code;
				if(!empty($result->data)){
					$msg['data'] = json_decode($result->data[0]['list']);
				}
			}
		}catch(Exception $ex){
			$msg['msg'] = $ex->getMessage();
		}
		return json_encode($msg);
	}

	//请假申请详情
	public function leaveInfo(){
		$msg =  ['msg'=>'请求网络失败，请检查手机网络状态','code'=>'400','data'=>[]];
		#其他参数
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : '';  #平台id
		$userId = $this->request->param('userId') ? getStr($this->request->param('userId')) : ''; #用户id
		$unitId =  $this->request->param('unitId') ? getStr($this->request->param('unitId')) : ''; #单位id
		$recordId =  $this->request->param('recordId') ? getStr($this->request->param('recordId')) : '';#请假申请记录id
		$cardId =  $this->request->param('cardId') ? getStr($this->request->param('cardId')) : '';# 审批时传入

		#验证token userId 和 token 必传
		$token = $this->request->param('token') ? getStr($this->request->param('token')) : '';
		if(!checkToken($token,$userId)){$msg['msg'] ='用户登录失效，请重新登录'; $msg['code']=501; return json_encode($msg);}
		if(empty($platformId)){$msg['msg'] = "请选择正确的平台"; return json_encode($msg);}

		try{
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAppLeaveServiceClient(NULL);
			\ApiClient::build($api);
			#sdkData 
			$sdkData = ['recordId'=>$recordId,'cardId'=>$cardId];
			$leaveDO = new LeaveDTO();
			$leaveDO->userId = $userId; 
			$leaveDO->platformId = $platformId;
			$leaveDO->unitId = $unitId; 
			$leaveDO->data = $sdkData;
			$result = $api->leaveInfo($leaveDO);
			if($result->success == true){
				$msg['msg'] = $result->message;
				$msg['code'] = $result->code;

				if(!empty($result->data)){
					$msg['data'] = json_decode($result->data[0]['data']);
				}
			}
		}catch(Exception $ex){
			$msg['msg'] = $ex->getMessage();
		}
		return json_encode($msg);
	}

	//请假申请催办
	public function leaveUrge(){
		$msg =  ['msg'=>'请求网络失败，请检查手机网络状态','code'=>'400','data'=>[]];
		#其他参数
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : '';  #平台id
		$userId = $this->request->param('userId') ? getStr($this->request->param('userId')) : ''; #用户id
		$unitId =  $this->request->param('unitId') ? getStr($this->request->param('unitId')) : ''; #单位id
		$recordId =  $this->request->param('recordId') ? getStr($this->request->param('recordId')) : '';#申请记录id

		#验证token userId 和 token 必传
		$token = $this->request->param('token') ? getStr($this->request->param('token')) : '';
		if(!checkToken($token,$userId)){$msg['msg'] ='用户登录失效，请重新登录'; $msg['code']=501; return json_encode($msg);}
		if(empty($platformId)){$msg['msg'] = "请选择正确的平台"; return json_encode($msg);}

		try{
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAppLeaveServiceClient(NULL);
			\ApiClient::build($api);
			#sdkData 
			$sdkData = ['recordId'=>$recordId];
			$leaveDO = new LeaveDTO();
			$leaveDO->userId = $userId; 
			$leaveDO->platformId = $platformId;
			$leaveDO->unitId = $unitId; 
			$leaveDO->data = $sdkData;
			$result = $api->leaveUrge($leaveDO); 
			if($result->success == true){
				$msg['msg'] = $result->message;
				$msg['code'] = $result->code; 
				if(!empty($result->data)){
					$msg['data'] = json_decode($result->data[0]['data']);
				}
			}
		}catch(Exception $ex){
			$msg['msg'] = $ex->getMessage();
		}
		return json_encode($msg);
	}

	//请假撤销
	public function leaveRevoke(){
		$msg =  ['msg'=>'请求网络失败，请检查手机网络状态','code'=>'400','data'=>[]];
		#其他参数
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : '';  #平台id
		$userId = $this->request->param('userId') ? getStr($this->request->param('userId')) : ''; #用户id
		$unitId =  $this->request->param('unitId') ? getStr($this->request->param('unitId')) : ''; #单位id
		$recordId =  $this->request->param('recordId') ? getStr($this->request->param('recordId')) : '';#申请记录id

		#验证token userId 和 token 必传
		$token = $this->request->param('token') ? getStr($this->request->param('token')) : '';
		if(!checkToken($token,$userId)){$msg['msg'] ='用户登录失效，请重新登录'; $msg['code']=501; return json_encode($msg);}
		if(empty($platformId)){$msg['msg'] = "请选择正确的平台"; return json_encode($msg);}

		try{
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAppLeaveServiceClient(NULL);
			\ApiClient::build($api);
			#sdkData 
			$sdkData = ['recordId'=>$recordId];
			$levaeDO = new LeaveDTO();
			$levaeDO->userId = $userId; 
			$levaeDO->platformId = $platformId;
			$levaeDO->unitId = $unitId; 
			$levaeDO->data = $sdkData;
			$result = $api->leaveRevoke($levaeDO); 
			if($result->success == true){
				$msg['msg'] = $result->message;
				$msg['code'] = $result->code; 
			}
		}catch(Exception $ex){
			$msg['msg'] = $ex->getMessage();
		}
		return json_encode($msg);
	}
	 
}
